<?php
namespace app\api\controller;

use think\Db;
class User extends Base {

    public function aliInfo() {
        try {
            if($this->userinfo['login_type'] === 3) {
                $whereUser = [
                    ['user_id','=',$this->userinfo['alipay_user_id']]
                ];
                $exist = Db::table('mp_user_alipay')->where($whereUser)->find();
                if($exist['uid']) {
                    $tel = Db::table('mp_user')->where('id','=',$exist['uid'])->value('tel');
                    $exist['tel'] = substr_replace($tel, '****', 3, 4);;
                }
                return ajax($exist);
            }else {
                return ajax('请用支付宝登录',37);
            }
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }

    }

    public function wxInfo() {
        try {
            if($this->userinfo['login_type'] === 2) {
                $whereUser = [
                    ['openid','=',$this->userinfo['wx_openid']]
                ];
                $exist = Db::table('mp_user_wechat')->where($whereUser)->find();
                if($exist['uid']) {
                    $tel = Db::table('mp_user')->where('id','=',$exist['uid'])->value('tel');
                    $exist['tel'] = substr_replace($tel, '****', 3, 4);;
                }
                mylog('wxInfo',var_export($exist,true),'test.log');
                return ajax($exist);
            }else {
                return ajax('请用支付宝登录',37);
            }
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }

    }

    public function orderList() {
        $uid = $this->userinfo['uid'];
        $type = input('post.type','');
        $curr_page = input('post.page',1);
        $perpage = input('post.perpage',10);
        $whereOrder = [
            ['uid','=',$uid],
            ['del','=',0]
        ];
        if($type) {
            if($type == 2) {
                $whereOrder[] = ['status','=',0];
            }
            if($type == 3) {
                $whereOrder[] = ['status','=',1];
                $whereOrder[] = ['refund','=',0];
            }
            if($type == 4) {
                $whereOrder[] = ['refund','=',2];
            }
        }
        try {
            $order_list = Db::table('mp_food_order')->where($whereOrder)
                ->order(['id'=>'DESC'])
                ->limit(($curr_page-1)*$perpage,$perpage)
                ->field('id,pay_order_sn,pay_price,status,refund,type,shop_name,table_no,number')
                ->select();
            if(empty($order_list)) {
                $detail_list = [];
            }else {
                $order_ids = [];
                foreach ($order_list as $o) {
                    $order_ids[] = $o['id'];
                }
                $whereDetail = [
                    ['order_id','in',$order_ids]
                ];
                $detail_list = Db::table('mp_food_order_detail')
                    ->field('order_id,food_id,food_name,num,unit_price,total_price,pic')
                    ->where($whereDetail)
                    ->select();
            }
        } catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        $ret = [
            'order_list' => $order_list,
            'detail_list' => $detail_list
        ];
        return ajax($ret);
    }

    public function orderDetail() {
        $post['pay_order_sn'] = input('post.pay_order_sn');
        checkPost($post);
        try {
            $whereOrder = [
                ['pay_order_sn','=',$post['pay_order_sn']]
            ];
            $order = Db::table('mp_food_order')->where($whereOrder)->find();
            if(!$order) {
                return ajax('订单不存在',27);
            }
            $detail = Db::table('mp_food_order_detail')->where('order_id','=',$order['id'])->select();
        } catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        $order['detail'] = $detail;
        return ajax($order);
    }



}